package com.i2works.smartluolong.web.controller.index;


import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.carousel.CarouselDO;
import com.i2works.smartluolong.web.entity.carousel.CarouselQuery;
import com.i2works.smartluolong.web.entity.org.OrgDO;
import com.i2works.smartluolong.web.entity.org.OrgQuery;
import com.i2works.smartluolong.web.service.carousel.ICarouselService;
import com.i2works.smartluolong.web.service.org.IOrgService;
import com.i2works.smartluolong.web.utils.BaseModule;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 主页控制器
 *
 * @author koal <koal@vip.qq.com>
 * @date 2017/9/27 20:07
 */

@Controller
@RequestMapping
public class IndexController extends BaseModule {
	
	@Autowired
	private  ICarouselService carouselService;
	
	@Autowired
	private IOrgService orgService;



    /**
     * 主页
     */
    @GetMapping(value = {"", "/", "/index"})
    public String index(Model model) {
    	try {
    		
    		 Session session = SecurityUtils.getSubject().getSession();
    	       if(session.getAttribute("windowOffices")!=null) {
    	    	   session.removeAttribute("windowOffices");
    	       }
    	       if(session.getAttribute("adminUserId")!=null) {
    	    	   session.removeAttribute("adminUserId");
    	       }
    	       if(session.getAttribute("adminUserName")!=null) {
    	    	   session.removeAttribute("adminUserName");
    	       }
    		
       /**
        * 轮播图
        * */
      
    	CarouselQuery carouselQuery=new CarouselQuery();
    	carouselQuery.setStatus(1);
    	carouselQuery.setType(1);
    	List<CarouselDO> carouselList=carouselService.queryList(carouselQuery);
    	model.addAttribute("carouselList",carouselList);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
    	
        return "/index";
    }

    /**
     * 
     * 二级页面
     */
   @PostMapping("/deptList")
   @ResponseBody
    public Result list(OrgQuery query) {
    	try {
    	List<OrgDO> list=Lists.newArrayList();
    	query.setStatus(1);
    	query.setLimit(500L);
    	Integer count=	orgService.queryCount(query);
    	if(count>0) {
    		list=	orgService.queryList(query);
    	}
    	return R.success(list,count);
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}
    	
    }


    
    /**
     * 
     * 广告副屏
     */
    @GetMapping("/ad/{mTag}")
    public String ad(@PathVariable("mTag") String  mTag,Model model) {
    	model.addAttribute("mTag",mTag);
    	return "screen/advertising";
    }
    /**
     * 
     * 操作说明
     */
    @GetMapping("/howToPlay")
    public String howToPlay(Model model) {
    	return "howToPlay/index";
    }

    /**
     * 
     * 操作说明-公司工商执照申请
     */
    @GetMapping("/gszz")
    public String gszz(Model model) {
    	return "howToPlay/gszz";
    }
    /**
     * 
     * 操作说明-个体工商执照申请
     */
    @GetMapping("/gtzz")
    public String gtzz(Model model) {
    	return "howToPlay/gtzz";
    }
    /**
     * 
     * 操作说明-身份证打印
     */
    @GetMapping("/sfz")
    public String sfz(Model model) {
    	return "howToPlay/sfz";
    }
    /**
     * 
     * 操作说明-注册登陆
     */
    @GetMapping("/zcdl")
    public String zcdl(Model model) {
    	return "howToPlay/zcdl";
    }
}
